﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AUV5.Web.Pages
{
    public partial class Register : PageBaseFrontend
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (base.IsVerified)
            {
                Response.Redirect(URL_INDEX);
            }
        }

        protected void lbtnRegist_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.txtEmail.Text.Trim()))
            {
                this.lblMessage.Text = "邮箱不能为空";
                return;
            }

            if (string.IsNullOrWhiteSpace(this.txtUserName.Text.Trim()))
            {
                this.lblMessage.Text = "用户名不能为空";
                return;
            }
            else
            {
                //用户名不能包含敏感字符
                if (BadWordsFilter.Instance.HasBadWord(txtUserName.Text.Trim()))
                {
                    string badword = BadWordsFilter.Instance.FindOne(txtUserName.Text.Trim());
                    this.lblMessage.Text = "用户名包含有敏感字符\"" + badword + "\"";
                    return;
                }
            }

            if (string.IsNullOrWhiteSpace(this.txtPassword.Text.Trim()))
            {
                this.lblMessage.Text = "密码不能为空";
                return;
            }
            if (txtPassword.Text.Trim() != txtConfirmPassword.Text.Trim())
            {
                this.lblMessage.Text = "两次输入密码不一致";
                return;
            }
            if (txbResult.Text.Trim() != Convert.ToString(Session["MathIdentifyResult"]))
            {
                this.lblMessage.Text = "数学题算错了";
                txbResult.Text = "";
                return;
            }

            Model.UserInfo user = new Model.UserInfo();
            user.Email = this.txtEmail.Text.Trim();
            user.UserName = this.txtUserName.Text.Trim();
            user.Password = EncryptMD5(this.txtPassword.Text.Trim());

            string userID = string.Empty;
            string resultOne = BLL.UserInfoBLL.Instance.FirstStepOfRegister(user, out userID);

            if (resultOne == "100")
            {
                //100:成功，110：服务器错误，201：邮箱已存在，202：用户名已存在
                Model.UserField userField = new Model.UserField();
                userField.FieldID = Guid.NewGuid().ToString();
                userField.UserID = userID;

                //string resultTwo =
                BLL.UserFieldBLL.Instance.SecondStepOfRegister(userField);

                //注册完成后自动登录
                base.VerifiedInfo = new AUV5.Model.VerifiedInfo(user);
                //显示成功信息
                base.SuccessInfo = string.Format("注册成功！");

                Response.Redirect(URL_INDEX);
            }
            else
            {
                //100:成功，110：服务器错误，201：邮箱已存在，202：用户名已存在
                switch (resultOne)
                {
                    case "110": resultOne = "服务器错误！"; break;
                    case "201": resultOne = "邮箱已存在"; break;
                    case "202": resultOne = "用户名已存在"; break;
                    default:
                        break;
                }
                this.lblMessage.Text = resultOne;
            }
        }
    }
}